package com.lanswon.controller.app;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.MessageType;
import com.lanswon.common.enums.SexType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.UserVO;
import com.lanswon.entity.Verification;
import com.lanswon.qzsmk.service.commonservice.Personal;
import com.lanswon.service.CoreService;
import com.lanswon.service.UserCardService;
import com.lanswon.service.UserService;
import com.lanswon.service.VerificationService;

import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: AppController.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: zm
 * @date: 2018年5月11日 上午11:02:18
 *
 */
@Controller
@RequestMapping("app/")
public class AppController {

	@Autowired
	CoreService coreService;

	@Autowired
	UserService userService;
	
	@Autowired
	private VerificationService verificationService;

	// 1.查询需要挂失卡信息
	@RequestMapping("queryLossCard")
	@ResponseBody
	public JSONObject queryLossCard(HttpServletRequest request, int startLine, int numPerPage, int currentPage,
			String token,Long userId) throws IOException {
		JSONObject jsonObject = new JSONObject();
		request.setCharacterEncoding("utf-8");
		//Long userId = (Long) request.getSession().getAttribute("userId");
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}

		return coreService.queryLossCard(startLine, numPerPage, currentPage, userId);
	}

	// 2.提交挂失信息
	@RequestMapping("commitLossCardInfo")
	@ResponseBody
	public JSONObject commitLossCardInfo(HttpServletRequest request, String citizenCardNo, String cardPName,
			String cardStatus, String token,Long userId) throws IOException {
		JSONObject jsonObject = new JSONObject();
		request.setCharacterEncoding("utf-8");
		// 传参校验
		if (StringUtil.isEmpty(citizenCardNo) || StringUtil.isEmpty(token) || StringUtil.isEmpty(cardPName)
				|| StringUtil.isEmpty(cardStatus)) {
			jsonObject.put("code", 5);
			jsonObject.put("message", MessageType.MT001);
			return jsonObject;
		}
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		
		return coreService.commitLossCardInfo(citizenCardNo, cardPName, cardStatus, userId);
	}

	// 3.查询需要补卡信息
	@RequestMapping("querySupplementCard")
	@ResponseBody
	public JSONObject querySupplementCard(HttpServletRequest request, int startLine, int numPerPage, int currentPage,
			String token,Long userId) throws IOException {
		JSONObject jsonObject = new JSONObject();
		request.setCharacterEncoding("utf-8");
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}

		return coreService.querySupplementCard(startLine, numPerPage, currentPage, userId);
	}

	// 4.提交补卡请求
	@RequestMapping("commitSupplementCardInfo")
	@ResponseBody
	public JSONObject commitSupplementCardInfo(HttpServletRequest request, String oldCitizenCardNo, String token
			,Long userId)
			throws IOException {
		JSONObject jsonObject = new JSONObject();
		request.setCharacterEncoding("utf-8");
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}

		return coreService.commitSupplementCardInfo(oldCitizenCardNo, userId);
	}

	// 5.查询网点
	@RequestMapping("queryBranch")
	@ResponseBody
	public JSONObject queryBranch(String streetId, String orgProperty) {
		return coreService.queryBranch(streetId, orgProperty);
	}

	// 5.查询网点
	@RequestMapping("testWS")
	@ResponseBody
	public JSONObject testWS() {
		return coreService.testWS();
	}
	
	// 6.验证能否办理相关卡产品
	@RequestMapping("checkIsCardProductbyPaperNo")
	@ResponseBody
	public JSONObject checkIsCardProductbyPaperNo(HttpServletRequest request, String paperType, String paperNo,
			String cardProductCode, String token,Long userId) throws IOException {
		JSONObject jsonObject = new JSONObject();
		request.setCharacterEncoding("utf-8");
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		// 传参校验
		if (StringUtil.isEmpty(paperType) || StringUtil.isEmpty(paperNo)) {
			jsonObject.put("code", 5);
			jsonObject.put("message", MessageType.MT_SR_1);
			return jsonObject;
		}
		if (StringUtil.isEmpty(token) || StringUtil.isEmpty(cardProductCode)) {
			jsonObject.put("code", 6);
			jsonObject.put("message", MessageType.MT_SR_7);
			return jsonObject;
		}

		return coreService.checkIsCardProductbyPaperNo(cardProductCode, paperType, paperNo, userId);
	}

	/**
	 * 12申请查询
	 * 
	 * @return
	 */
	@RequestMapping("smkApplication")
	@ResponseBody
	public JSONObject smkApplication(String token, HttpServletRequest request
			,Long userId) {
		JSONObject jsonObject = new JSONObject();
		
		// 验证信息是否完善
		try {
			// 传参校验
			if(userId==null||StringUtil.isEmpty(token)){
				jsonObject.put("code", 1);
				jsonObject.put("message", "参数为空");
				return jsonObject;
			}
			UserVO user = userService.getUser(userId);
			if(user==null){
				jsonObject.put("code", 2);
				jsonObject.put("message", "用户不存在或未登录");
				return jsonObject;
			}
			Verification verify = verificationService.findVerificationByPhone(user.getMobile());
			if(verify==null||!verify.getToken().equals(token)){
				jsonObject.put("code", 3);
				jsonObject.put("message", "验证过期，请重新登录");
				return jsonObject;
			}
			if (StringUtil.isEmpty(user.getIdNum())) {
				jsonObject.put("code", 4);
				jsonObject.put("message", "用户信息不完整");
				return jsonObject;
			}
			
			jsonObject = coreService.smkApplication(userId);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("code", 99);
			jsonObject.put("message", "查询失败");
			return jsonObject;
		}
		return jsonObject;
	}

	// 7.个人申请提交 singleApplication
	// http://localhost:8080/qzsmk/app/singleApplication.do?
	@RequestMapping("singleApplication")
	@ResponseBody
	public JSONObject singleApplication(Personal p, String cardProductCode, String cardPhysicalType, String token,
			HttpServletRequest request, String birthdayStr,Long userId) throws ParseException, DatatypeConfigurationException {

		JSONObject jsonObject = new JSONObject();
		
		try {
			// 传参校验
			if(userId==null||StringUtil.isEmpty(token)){
				jsonObject.put("code", 1);
				jsonObject.put("message", "参数为空");
				return jsonObject;
			}
			UserVO user = userService.getUser(userId);
			if(user==null){
				jsonObject.put("code", 2);
				jsonObject.put("message", "用户不存在或未登录");
				return jsonObject;
			}
			Verification verify = verificationService.findVerificationByPhone(user.getMobile());
			if(verify==null||!verify.getToken().equals(token)){
				jsonObject.put("code", 3);
				jsonObject.put("message", "验证过期，请重新登录");
				return jsonObject;
			}
			// 验证信息是否完善
			if (StringUtil.isEmpty(user.getIdNum())) {
				jsonObject.put("code", 4);
				jsonObject.put("message", "用户信息不完整");
				return jsonObject;
			}
			if (!p.getPaperNo().equals(user.getIdNum())) {
				jsonObject.put("code", 5);
				jsonObject.put("message", "申请证件号与身份证号不一致");
				return jsonObject;
			}

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			GregorianCalendar gcDate = new GregorianCalendar();
			gcDate.setTime(sdf.parse(birthdayStr));
			XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcDate);
			p.setBirthday(xmlGregorianCalendar);
			// 性别判断
			if (p.getGender().equals(SexType.SEX_FEMALE.getValue())) {
				p.setGender(String.valueOf(SexType.SEX_FEMALE.getCode()));
			} else {
				p.setGender(String.valueOf(SexType.SEX_MAN.getCode()));
			}
			jsonObject = coreService.singleApplication(p, cardProductCode, cardPhysicalType, userId);

		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("code", 99);
			jsonObject.put("message", "申请失败");
			return jsonObject;
		}
		return jsonObject;
	}

	/**
	 * 8基础信息查询（证件类型）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCertificates")
	@ResponseBody
	public Map<String, Object> queryAllCertificates(HttpServletRequest request,Long userId, String token) {
		JSONObject jsonObject = new JSONObject();
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		return coreService.queryAllCertificates();

	}

	/**
	 * 9基础信息查询（卡产品）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCardProdunctDefinition")
	@ResponseBody
	public Map<String, Object> queryAllCardProdunctDefinition(HttpServletRequest request,String userId, String token) {
		/*JSONObject jsonObject = new JSONObject();
		Long _userId = (Long) request.getSession().getAttribute("userId");
		UserVO user = userService.getUser(_userId);
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 1);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}*/
		return coreService.queryAllCardProdunctDefinition();

	}

	/**
	 * 10基础信息查询（卡物理类型）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCardPhysicsType")
	@ResponseBody
	public Map<String, Object> queryAllCardPhysicsType(HttpServletRequest request,Long userId, String token ) {
		JSONObject jsonObject = new JSONObject();
		
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		return coreService.queryAllCardPhysicsType();

	}

	/**
	 * 11基础信息查询(个人信息)
	 * 
	 * @return
	 */
	@RequestMapping("queryPersonalByPaperNoAndPaperType")
	@ResponseBody
	public JSONObject queryPersonalByPaperNoAndPaperType(String paperType, String paperNo, HttpServletRequest request,
			String token,Long userId) {
		JSONObject jsonObject = new JSONObject();
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		/*
		 * paperType = "00"; paperNo = "320925198607224192";
		 */

		return coreService.queryPersonalByPaperNoAndPaperType(paperType, paperNo);

	}

	/**
	 * 12基础信息查询(卡相关信息查询)
	 */
	@RequestMapping("queryPersonalCardInfoAndConsumeRecordsByPaperNo")
	@ResponseBody
	public JSONObject queryPersonalCardInfoAndConsumeRecordsByPaperNo(String citizenCardNo, String year,
			String month, String transactionType,String appFlag, HttpServletRequest request, String token,
			 Long userId) {
		JSONObject jsonObject = new JSONObject();
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)||StringUtil.isEmpty(citizenCardNo)||
				StringUtil.isEmpty(transactionType)||StringUtil.isEmpty(appFlag)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		return coreService.queryPersonalCardInfoAndConsumeRecordsByPaperNo( citizenCardNo,  year,
				 month,  transactionType, appFlag);
	}

	/**
	 * 13基础信息查询(卡申请进度查询)
	 */
	@RequestMapping("queryApplySchedule")
	@ResponseBody
	public JSONObject queryApplySchedule(HttpServletRequest request, String token, String queryCondition
			,Long userId) {
		JSONObject jsonObject = new JSONObject();
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		/*
		 * paperType = "00"; paperNo="342401198912108579"; cardPCode = "80";
		 */
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 5);
			jsonObject.put("message", "无卡产品");
			return jsonObject;
		}
		return coreService.queryApplySchedule(user.getIdNum(), user.getPapertype(), queryCondition);
	}

	/**
	 * 14 基础信息查询(通过挂失凭证号查询)
	 */
	@RequestMapping("queryPersonal")
	@ResponseBody
	public JSONObject queryPersonal(String certNo, HttpServletRequest request, String token
			,Long userId) {
		JSONObject jsonObject = new JSONObject();
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		// certNo = "1da14c98569c11e8b74254ee75d9af4c";
		return coreService.queryPersonal(certNo);
	}

	/**
	 * 16基础信息查询(交易查询前,获取用户下所有卡产品)
	 * 
	 * @return
	 */
	@RequestMapping("queryCardProdunctByPersonal")
	@ResponseBody
	public JSONObject queryCardProdunctByPersonal(HttpServletRequest request, String cardPCode, String token
			,Long userId) {
		JSONObject jsonObject = new JSONObject();
		// 登陆校验
		// 传参校验
		if(userId==null||StringUtil.isEmpty(token)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "参数为空");
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if(user==null){
			jsonObject.put("code", 2);
			jsonObject.put("message", "用户不存在或未登录");
			return jsonObject;
		}
		Verification verify = verificationService.findVerificationByPhone(user.getMobile());
		if(verify==null||!verify.getToken().equals(token)){
			jsonObject.put("code", 3);
			jsonObject.put("message", "验证过期，请重新登录");
			return jsonObject;
		}
		// 验证信息是否完善
		if (StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "用户信息不完整");
			return jsonObject;
		}
		/*
		 * String paperType = "00"; String paperNo = "320925198809303619";
		 * cardPCode = "10";
		 */
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getIdNum())) {
			jsonObject.put("code", 40);
			jsonObject.put("message", "未绑定证件");
			return jsonObject;
		}

		return coreService.queryCardProdunctByPersonal(user.getPapertype(), user.getIdNum(), cardPCode);
	}

	// 17.查询网点
	@RequestMapping("queryAllBranchStreet")
	@ResponseBody
	public JSONObject queryAllBranchStreet() {
		return coreService.queryAllBranchStreet();
	}

	// 18.查询网点
	@RequestMapping("queryOrgProperty")
	@ResponseBody
	public JSONObject queryOrgProperty() {
		return coreService.queryOrgProperty();
	}

	// 19.查询详情
	@RequestMapping("queryBranchByName")
	@ResponseBody
	public JSONObject queryBranchByName(String branchName) {
		return coreService.queryBranchByName(branchName);
	}

	// 20.查询名族
	@RequestMapping("queryNation")
	@ResponseBody
	public JSONObject queryNation() {
		return coreService.queryNation();
	}
	
	
	
}
